﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;

namespace BI
{
    public class BaseStrategy
    {
        private string[] bestCards = null;
        private string[] goodCards = null;
        private string[] mediumCards = null;

        public BaseStrategy()
        {
            bestCards = new string[]
                            {
                                "AA",
                                "KK",
                                "QQ",
                                "JJ",
                                "AK","KA",
                                "KQ","QK",
                                "AQ","QA","AJ","JA",
                            };

            goodCards = new string[]
                            {
                                
                                
                            };

            mediumCards = new string[]
                            {
                                "A10","10A","A9","9A",
                                "KJ","JK",
                                "8A","A8","A7","7A",
                                "K10","10K","K9","9K","8K","K8",
                                "J10","10J","QJ","JQ",
                                "1010",
                            };
        }

        public void MakeDecision(PokerTable pokerTable)
        {
            TableAction result = TableAction.Fold;
            switch (pokerTable.TableState)
            {
                case TableState.PreFlop:
                    result = MakeDecisionInPreFlop(pokerTable);
                    break;
                case TableState.PostFlop:
                    result = MakeDecisionInPostFlop(pokerTable);
                    break;
                case TableState.Turn:
                    result = MakeDecisionInTurn(pokerTable);
                    break;
                case TableState.River:
                    result = MakeDecisionInRiver(pokerTable);
                    break;
            }

            pokerTable.Action = result;
        }

        private TableAction MakeDecisionInPreFlop(PokerTable pokerTable)
        {
            TableAction result = TableAction.Fold;

            HandStrength handStrength = holdGoodCards(pokerTable.HoleCards);

            if (handStrength == HandStrength.Hard)
            {
                if (pokerTable.TableActionButtons.BetButtonEnabled)
                {
                    result = TableAction.Bet;
                }
                else if (pokerTable.TableActionButtons.RaiseButtonEnabled)
                {
                    result = TableAction.Raise;
                }
                else if (pokerTable.TableActionButtons.CallButtonEnabled)
                {
                    result = TableAction.Call;
                }
                else if (pokerTable.TableActionButtons.CallButtonEnabled)
                {
                    result = TableAction.Check;
                }
            }
            else if (handStrength == HandStrength.MediumHard)
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
                else if (pokerTable.TableActionButtons.CallButtonEnabled)
                {
                    result = TableAction.Call;
                }
            }
            else if (handStrength == HandStrength.Medium && (pokerTable.TableActionButtons.CallButtonChips <= pokerTable.Blinds.SmallBlind) && pokerTable.TableActionButtons.CallButtonEnabled)
            {
                result = TableAction.Call;
            }
            else if (handStrength == HandStrength.Medium)
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
            }
            else if (handStrength == HandStrength.Weak && pokerTable.TableActionButtons.CheckButtonEnabled)
            {
                result = TableAction.Check;
            }
            decimal smallBlind = 0.01m;
            decimal smallBlind2 = 0.013m;

            if ((result == TableAction.Fold) && (pokerTable.TableActionButtons.CallButtonEnabled) && ((pokerTable.TableActionButtons.CallButtonChips == smallBlind) || (pokerTable.TableActionButtons.CallButtonChips == smallBlind2)))
            {
                result = TableAction.Call;
            }

            return result;
        }

        private HandStrength holdGoodCards(HoleCards holeCards)
        {
            HandStrength result = HandStrength.Weak;

            string holeCardsRanks = holeCards.GetSimpleCardRank();

            if (bestCards.Contains(holeCardsRanks))
            {
                result = HandStrength.Hard;
            }
            else if (goodCards.Contains(holeCardsRanks))
            {
                result = HandStrength.MediumHard;
            }
            else if (mediumCards.Contains(holeCardsRanks))
            {
                result = HandStrength.Medium;
            }

            return result;
        }

        private TableAction MakeDecisionInPostFlop(PokerTable pokerTable)
        {
            return MakePostFlopDecision(pokerTable);
        }

        private TableAction MakeDecisionInRiver(PokerTable pokerTable)
        {
            return MakeRiverDecision(pokerTable);
        }

        private TableAction MakeDecisionInTurn(PokerTable pokerTable)
        {
            return MakeTurnDecision(pokerTable);
        }

        private TableAction MakePostFlopDecision(PokerTable pokerTable)
        {
            TableAction result = TableAction.Fold;

            if (pokerTable.HeroWinRate >= 65)
            {
                if (pokerTable.TableActionButtons.BetButtonEnabled)
                {
                    result = TableAction.Bet;
                }
                else if (pokerTable.TableActionButtons.RaiseButtonEnabled && (pokerTable.HeroWinRate > 80))
                {
                    result = TableAction.Raise;
                }
                else
                {
                    result = TableAction.Call;
                }
            }
            else if (pokerTable.HeroWinRate > 40 && pokerTable.HeroWinRate < 65)
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
                else if (pokerTable.TableActionButtons.CallButtonEnabled)
                {
                    result = TableAction.Call;
                }
                else
                {
                    result = TableAction.Fold;
                }
            }
            else
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
                else
                {
                    result = TableAction.Fold;
                }
            }

            return result;
        }

        private TableAction MakeTurnDecision(PokerTable pokerTable)
        {
            TableAction result = TableAction.Fold;

            if (pokerTable.HeroWinRate >= 65)
            {
                if (pokerTable.TableActionButtons.BetButtonEnabled)
                {
                    result = TableAction.Bet;
                }
                else if (pokerTable.TableActionButtons.RaiseButtonEnabled && (pokerTable.HeroWinRate > 85))
                {
                    result = TableAction.Raise;
                }
                else
                {
                    result = TableAction.Call;
                }
            }
            else if (pokerTable.HeroWinRate > 45 && pokerTable.HeroWinRate < 65)
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
                else if (pokerTable.TableActionButtons.CallButtonEnabled)
                {
                    result = TableAction.Call;
                }
                else
                {
                    result = TableAction.Fold;
                }
            }
            else
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
                else
                {
                    result = TableAction.Fold;
                }
            }

            return result;
        }


        private TableAction MakeRiverDecision(PokerTable pokerTable)
        {
            TableAction result = TableAction.Fold;

            if (pokerTable.HeroWinRate >= 75)
            {
                if (pokerTable.TableActionButtons.BetButtonEnabled)
                {
                    result = TableAction.Bet;
                }
                else if (pokerTable.TableActionButtons.RaiseButtonEnabled && (pokerTable.HeroWinRate > 85))
                {
                    result = TableAction.Raise;
                }
                else
                {
                    result = TableAction.Call;
                }
            }
            else if (pokerTable.HeroWinRate > 50 && pokerTable.HeroWinRate < 75)
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
                else if (pokerTable.TableActionButtons.CallButtonEnabled)
                {
                    result = TableAction.Call;
                }
                else
                {
                    result = TableAction.Fold;
                }
            }
            else
            {
                if (pokerTable.TableActionButtons.CheckButtonEnabled)
                {
                    result = TableAction.Check;
                }
                else
                {
                    result = TableAction.Fold;
                }
            }

            return result;
        }
    }
}
